﻿using System;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.Common.Config;
using Bingosoft.Modules.SecurityConsole.User;
using Bingosoft.Security.Web.UI;
using Bingosoft.Web.Utils;
using HHMMS.Core;
using System.Data;

namespace Bingosoft.WebApp.Modules.SecurityConsole.User
{
    [UISecurity]
    public partial class UserList : System.Web.UI.BasePage
    {
        private UserService _userServie;
        private UserService UserService
        {
            get
            {
                return _userServie ??
                       (_userServie = ObjectHelper.GetObject<UserService>());
            }
        }

        protected void DeleteUser(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string userId = img.Attributes["_KeyId"];
            this.UserService.DeleteUser(userId);
            this.GridSearch.Refresh();
        }

        protected void EnableUser(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string userId = img.Attributes["_KeyId"];
            this.UserService.EnableAccount(new string[] { userId });
            this.GridSearch.Refresh();
        }

        protected void DisableUser(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string userId = img.Attributes["_KeyId"];
            this.UserService.DisableAccount(new string[] { userId });
            this.GridSearch.Refresh();
        }

        protected void DeleteUsers(object sender, EventArgs e)
        {
            string[] userIds = SmartGridView1.GetCheckedValues();

            foreach (string userId in userIds)
            {
                this.UserService.DeleteUser(userId);
            }
            this.GridSearch.Refresh();
        }

        protected void SearchGrid(object sender, EventArgs e)
        {
            this.GridSearch.Search();
        }

        protected void RefreshGrid(object sender, EventArgs e)
        {
            this.GridSearch.Refresh();
        }

        protected void SmartGridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var dataRow = e.Row.DataItem as DataRowView;
                ImageButton enableImgBtn = e.Row.FindControl("EnableImageButton") as ImageButton;
                ImageButton disableImgBtn = e.Row.FindControl("DisableImageButton") as ImageButton;
                if (enableImgBtn != null)
                {
                    string status = enableImgBtn.Attributes["_STATUS"];
                    if (Constants.USER_STATUS_ENABLED.Equals(status))
                    {
                        enableImgBtn.Visible = false;
                        if (disableImgBtn != null) disableImgBtn.Visible = true;
                    }
                    else
                    {
                        enableImgBtn.Visible = true;
                        if (disableImgBtn != null) disableImgBtn.Visible = false;
                    }
                }

                //超级管理员不能删除
                ImageButton ibtnDelete = e.Row.FindControl("ibtnDelete") as ImageButton;
                if (ibtnDelete != null)
                {
                   ibtnDelete.Visible = !Const.SUPER_ADMIN_LOGINID.Equals(dataRow["ACCOUNT"] + "", StringComparison.OrdinalIgnoreCase);
                }
            }
        }

    }
}